/**
 * Created by zhanglihe on 15/11/4.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'bootstrap',
    'collection',
    'template',
    'fineuploader'
], function( $, common, ready, bootstrap, Collection, template, Uploader ){

    var index = function() {
        this.init();
    };

    //是否开启多图上传
    var multiple = getUrlPara('multiple') ? true : false;

    //已选图片集合(供父窗口调用)
    window.affix = {
        data: {},
        set: function(id, val) {
            if(multiple) {
                this.data[id] = val;
            } else {
                this.data = val;
            }
            console.log(this.data);
        },
        get: function() {
            return this.data;
        },
        del: function(id) {
            if(multiple) {
                delete this.data[id];
            } else {
                this.data = {};
            }
        },
        clear: function() {
            this.data = {};
        }
    };

    index.prototype = {
        init: function() {
            this.upload();
            this.loadImage();
            this.event();
        },
        upload: function() {
            var that = this;
            $('#upload-view').html(template('upload-tpl'));

            var _path =typeof img_path !== 'undefined' ? img_path :'';
            //上传相关
            $("#J_UploadImgs").fineUploader({
                request: {
                    endpoint: window._global.url.api+'attachment/upload',
                    inputName: 'filedata',
                    params: {
                        access_token: window._global.access_token,
                        folder: _path
                    }
                },
                multiple: multiple,
                deleteFile: {
                    enabled: true,
                    endpoint: window._global.url.api+'attachment'
                },
                callbacks: {
                    onProgress: function(id, filename, loaded, total){
                        var progress = parseInt(loaded / total * 100, 10);
                        //$('#qq-file-id-'+id+' .upload-progress-bar').css('width',progress+ '%');
                        $('#img-view-'+id).html('进度：'+progress+ '%')
                    },
                    onSubmit: function(id, filename) {
                        $('#J_ImgList').append('<li class="upload-loading" id="img-view-'+id+'">正在上传中...</li>');
                    }
                }
            }).on('complete', function(event, id, fileName, response) {
                if (response.success) {
                    console.log('response.data',response.data);
                    var imgItem = $(template('img-item-tpl',response.data));
                    window.affix.set(response.data.id, {id: response.data.id, fileUrl: response.data.fileUrl});
                    //$(event.target).parent().addClass('selected');
                    $('#img-view-'+id).replaceWith(imgItem);
                    !multiple && $('.qq-upload-button-selector').hide();
                }else{
                    alert(response.msg);
                }
            });

            $('#J_ImgList').on('click', '.J_Delete', function() {
                var _$item = $(this).parents('li')
                    ,img_id = _$item.attr('data-id');
                $.ajax({
                    async:false,
                    url: window._global.url.api + 'attachment/'+img_id,
                    type: 'DELETE',
                    success: function(response) {
                        _$item.remove();
                        !multiple && $('.qq-upload-button-selector').show();
                    }
                });
            });
        }
    };

    new index();

});